package com.prj.ufdm.cloud.ms.auth.api;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.prj.ufdm.cloud.base.dto.RetData;
import com.prj.ufdm.cloud.base.exception.BaseException;
import com.prj.ufdm.cloud.base.util.BaseUuidUtil;
import com.prj.ufdm.cloud.ms.auth.enums.AuthRetCodeEnum;
import com.prj.ufdm.cloud.ms.core.bpo.code.SysCodeItemBPO;
import com.prj.ufdm.cloud.ms.core.model.PageBean;
import com.prj.ufdm.cloud.ms.core.model.code.SysCodeItem;
import com.prj.ufdm.cloud.ms.core.model.code.SysCodeItemVO;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhengzixiang
 * @since 2019-09-20
 */
@RestController
@RequestMapping("/api/auth/sysCodeItem")
public class SysCodeItemController {

 	@Autowired
    private SysCodeItemBPO sysCodeItemBPO;
    
    /**
   	 *查询列表
    */
    @RequestMapping("/doGetList")
	public RetData<PageInfo<SysCodeItem>> doGetList(PageBean pageBean,SysCodeItem t)  throws Exception {
		RetData<PageInfo<SysCodeItem>> retData = new RetData<PageInfo<SysCodeItem>>();
		PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
		List<SysCodeItem>  list=sysCodeItemBPO.doGetList(t);
		PageInfo<SysCodeItem> page=new PageInfo<SysCodeItem>(list);
		retData.setRetBody(page);
		return retData;
	}
	 /**
   	 *通过id查询
    */
	@RequestMapping("/doGetById")
	public RetData<SysCodeItem> doGetById(String id)  throws Exception {
		RetData<SysCodeItem> retData = new RetData<SysCodeItem>();
		SysCodeItem result=sysCodeItemBPO.doGetById(id);
		retData.setRetBody(result);
		return retData;
	}
	/**
   	 *保存
    */
	@RequestMapping("/doSave")
	public RetData<SysCodeItem> doSave(SysCodeItem t)throws Exception{
		RetData<SysCodeItem> ret=new RetData<SysCodeItem>();
		if(StringUtils.isBlank(t.getSysCodeId())) {
			throw new BaseException(AuthRetCodeEnum.RET_CODE_0104006.getCode(), AuthRetCodeEnum.RET_CODE_0104006.getMessage());
		}
		
		if(StringUtils.isBlank(t.getCodeName())) {
			throw new BaseException(AuthRetCodeEnum.RET_CODE_0104004.getCode(), AuthRetCodeEnum.RET_CODE_0104004.getMessage());
		}
		if(StringUtils.isBlank(t.getCodeValue())) {
			throw new BaseException(AuthRetCodeEnum.RET_CODE_0104005.getCode(), AuthRetCodeEnum.RET_CODE_0104005.getMessage());
		}
		SysCodeItem obj=new SysCodeItem();
		obj.setCodeName(t.getCodeName());
		obj.setSysCodeId(t.getSysCodeId());
		obj.setCodeValue(t.getCodeValue());
		Integer total=0;
		if(StringUtils.isBlank(t.getId())) {
			total=sysCodeItemBPO.doGetTotal(obj);
			if(null!=total&&total.compareTo(0)>0) {
				throw new BaseException(AuthRetCodeEnum.RET_CODE_0104007.getCode(), "该参数类型下参数名称或参数值已存在请检查录入数据");
			}
			t.setId(BaseUuidUtil.getUUID());
			t.setOrdinal(sysCodeItemBPO.doGetMaxOrdinalBySysCodeId(t.getSysCodeId())+1);
			sysCodeItemBPO.doSave(t);
		}else {
			obj.setId(t.getId());
			total=sysCodeItemBPO.doGetNotMeTotal(obj);//查找code是否已被其他记录引用
			if(null!=total&&total.compareTo(0)>0) {
				throw new BaseException(AuthRetCodeEnum.RET_CODE_0104007.getCode(), "该参数类型下参数名称或参数值已存在请检查录入数据");
			}
			sysCodeItemBPO.doModById(t);
		}
		
		ret.setRetBody(t);
		return ret;
	}
	
	/**
   	 *更新
    */
	@RequestMapping("/doModById")
	public RetData<String> doModById(SysCodeItem t)throws Exception{
		RetData<String> ret=new RetData<String>();
		sysCodeItemBPO.doModById(t);
		ret.setRetBody("");
		return ret;
	}
	
	/**
   	 *删除
    */
	@RequestMapping("/doRmById")
	public RetData<String> doRmById(String id)throws Exception{
		RetData<String> ret=new RetData<String>();
		sysCodeItemBPO.doRmById(id);
		ret.setRetBody("");
		return ret;
	}
	
	/**
   	 *批量更新
    */
	@RequestMapping("/doModBatchById")
	public RetData<String> doModBatchById(@RequestBody SysCodeItemVO items)throws Exception{
		RetData<String> ret=new RetData<String>();
		List<SysCodeItem> ts = items.getCodeItems();
		for(SysCodeItem codeItem:ts) {
			if(StringUtils.isBlank(codeItem.getId())) {
				throw new BaseException(AuthRetCodeEnum.RET_CODE_0104010.getCode(), "有未保存的数据，请保存后再进行顺序变更");
			}
		}
		sysCodeItemBPO.doModBatchById(ts);
		ret.setRetBody("");
		return ret;
	}
	/**
	 * 根据枚举字查询枚举列表
	 * code 枚举关键字（必填）
   	 *查询列表
    */
    @RequestMapping("/doGetEnumList")
	public RetData<List<SysCodeItem>> doGetEnumList(String code,String enabledState)  throws Exception {
		RetData<List<SysCodeItem>> retData = new RetData<List<SysCodeItem>>();
		List<SysCodeItem>  list=sysCodeItemBPO.doGetEnumListByCodeAndEnabledState(code, enabledState);
		retData.setRetBody(list);
		return retData;
	}
}
